package com.wingsboat.common.protect.keyconvert.impl;

import cn.hutool.crypto.SecureUtil;
import com.wingsboat.common.protect.annotation.AthenaLock;
import com.wingsboat.common.protect.aspect.AthenaLockAspect;
import com.wingsboat.common.protect.keyconvert.KeyConvert;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 默认（全局级别）锁 Key 解析器，方法名 充当key
 *
 * 为了避免 Key 过长，使用 MD5 进行“压缩”
 * 此解析器 性能较低
 *
 * @author wangzhong
 */
@Component
public class DefaultAthenaLockKeyConvert implements KeyConvert, InitializingBean {

    @Autowired
    private AthenaLockAspect athenaLockAspect;


    @Override
    public void afterPropertiesSet() throws Exception {
        athenaLockAspect.addKeyConvert(this);
    }

    /**
     * 获取key
     *
     * @param joinPoint
     * @param athenaLock
     * @return
     */
    @Override
    public String getKey(ProceedingJoinPoint joinPoint, AthenaLock athenaLock) {
        String methodName = joinPoint.getSignature().toString();
        String key = SecureUtil.md5(methodName);
        return key;
    }
}
